package com.example.lol;

import android.content.Intent;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.lol.ui.FifthBottomFragment;
import com.example.lol.ui.FirstBottomFragment;
import com.example.lol.ui.SecondBottomFragment;
import com.example.lol.ui.ThirdBottomFragment;
import com.example.lol.ui.bean.TopCategoryBean;
import com.example.lol.ui.adapter.Adapter;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class lolActivity extends AppCompatActivity {
    private RecyclerView listView;
    private Adapter mNewsAdapter;
    private BottomNavigationView bottomNav;
    private LinearLayout topline;
    private ImageView left_button;
    private DrawerLayout dl;
    private TextView set_back;
    private final SparseArray<Fragment> mFragmentCache = new SparseArray<>();

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//设置界面的跳转
        set_back = findViewById(R.id.tv_setting_btn);
        set_back.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(lolActivity.this, SettingActivity.class);
                startActivity(intent);
            }
        });
        dl = findViewById(R.id.drawer_layout);
        left_button = findViewById(R.id.left_button);
        left_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dl.openDrawer(GravityCompat.START);
            }
        });
//左边个人名片内容
        listView = findViewById(R.id.recycle);
        listView.setLayoutManager(new LinearLayoutManager(this));
        mNewsAdapter = new Adapter();
        listView.setAdapter(mNewsAdapter);
        TopCategoryBean[] userBeans = new TopCategoryBean[7];
        for (int i = 0; i < 6; i++) {
            userBeans[i] = new TopCategoryBean();
        }
        userBeans[0].setUserName("我的评论");
        userBeans[1].setUserName("与我相关");
        userBeans[2].setUserName("我的订单");
        userBeans[3].setUserName("我的任务");
        userBeans[4].setUserName("我的收藏");
        userBeans[5].setUserName("活动中心");
        userBeans[0].setHeadId(R.drawable.one);
        userBeans[1].setHeadId(R.drawable.two);
        userBeans[2].setHeadId(R.drawable.three);
        userBeans[3].setHeadId(R.drawable.four);
        userBeans[4].setHeadId(R.drawable.five);
        userBeans[5].setHeadId(R.drawable.six);
        for (int i = 0; i <= 5; i++) {
            mNewsAdapter.addData(userBeans[i]);
        }
        mNewsAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                TopCategoryBean bean = mNewsAdapter.getItem(position);
                Toast.makeText(lolActivity.this, "" + userBeans[position].getUserName(), Toast.LENGTH_SHORT).show();
            }
        });
//底部BottomNavigationView导航
        bottomNav = findViewById(R.id.bottom_nav_view);
        topline = findViewById(R.id.top_line);//找到顶部菜单栏
        bottomNav.setOnNavigationItemSelectedListener(item -> {
            Fragment fragment = getContent(item);
            if (item.getItemId() != R.id.navigation_me) {
                topline.setVisibility(View.VISIBLE);//点击其他界面时显示顶部菜单栏
            } else if (item.getItemId() == R.id.navigation_me) {
                topline.setVisibility(View.GONE);//点击我的界面时隐藏顶部菜单栏
            }
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.frameLayout_bottom, fragment)//将FrameLayout中的内容切换
                    .commit();
            return true;
        });
        bottomNav.setSelectedItemId(R.id.navigation_zx);
    }

    private Fragment getContent(MenuItem menuItem) {
        int id = menuItem.getItemId();
        Fragment fragment = mFragmentCache.get(id);//从缓存中拿Fragment
        if (fragment == null) {
            fragment = nf(menuItem);
            mFragmentCache.put(id, fragment);//如果缓存中没有就新建一个Fragment
        }
        return fragment;
    }

    private Fragment nf(MenuItem menuItem) {
        Fragment fragment;
        switch (menuItem.getItemId()) {
            case R.id.navigation_zx:
                fragment = new FirstBottomFragment();
                break;
            case R.id.navigation_myq:
                fragment = new SecondBottomFragment();
                break;
            case R.id.navigation_hy:
                fragment = new ThirdBottomFragment();
                break;
            case R.id.navigation_shop:
                fragment = new FirstBottomFragment();
                break;
            case R.id.navigation_me:
                fragment = new FifthBottomFragment();
                break;
            default:
                fragment = new FirstBottomFragment();
        }
        return fragment;

    }
}
